package com.iwop.moto.controller;

import com.iwop.moto.comm.ErrCode;
import com.iwop.moto.model.ResultModel;
import com.iwop.moto.model.SuperRecordModel;
import com.iwop.moto.service.ShoppingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * liuwb 2016-07-29 14:01
 */
@Controller
public class ShoppingController extends  BaseController {

    @Autowired
    private ShoppingService shoppingService;

    /**
     ***************************************
     * 功能: 购物车 - 新增
     * liuwb 2016-07-29 14:23
     * 参数: userId 买家Id
     * 参数: goodsId 商品Id
     * 参数: goodsDetailId 商品规格id
     * 参数: nums 数量
     ***************************************
     */
    @RequestMapping("/api/shopping/add")
    @ResponseBody
    public ResultModel apiShoppingAdd(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            SRModel.put("sid", UUID.randomUUID().toString());
            SRModel.put("statusFlag","1");
            SRModel.put("statusTime",new Date());
            shoppingService.insertShopping(SRModel);
            return getSucceed(ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     ***************************************
     * 功能: 购物车 - 列表
     * liuwb 2016-07-29 14:23
     * 参数: userId 买家Id
     ***************************************
     */
    @RequestMapping("/api/shopping/list")
    @ResponseBody
    public ResultModel apiShoppingList(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            List<SuperRecordModel> lists = shoppingService.findShoppingList(SRModel);
            return getSucceed(lists);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     ***************************************
     * 功能: 购物车 - 删除
     * liuwb 2016-07-29 16:03
     * 参数: sid 主键
     ***************************************
     */
    @RequestMapping("/api/shopping/delete")
    @ResponseBody
    public ResultModel apiShoppingDelete(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            shoppingService.deleteShopping(SRModel);
            return getSucceed(ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }

    /**
     ***************************************
     * 功能: 购物车 - 修改数量
     * liuwb 2016-07-29 16:03
     * 参数: sid 主键
     * 参数: nums 修改数量
     ***************************************
     */
    @RequestMapping("/api/shopping/nums")
    @ResponseBody
    public ResultModel apiShoppingNums(){
        SuperRecordModel SRModel = this.getSRModel();
        try {
            shoppingService.updateShoppingNums(SRModel);
            return getSucceed(ErrCode.SUCCEED);
        } catch (Exception e) {
            e.printStackTrace();
            return getErr(ErrCode.ERROR_SYSTEM);
        }
    }


}
